import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/tabbar/work_space_model.dart';
import 'package:provider/provider.dart';
import 'horizontalBarChart.dart';
import 'routers/Routes.dart';
import 'routers/application.dart';
import 'package:flutter_module/request/NetRequest.dart';
import 'package:dio/dio.dart';
import 'package:flutter_module/tabbar/work_space_provider.dart';
import '../common/views/charts/horizontalBarChart.dart';

  class HomeScreen extends StatefulWidget {
    @override
    _HomeScreenState createState() => _HomeScreenState();
  }
  
  class _HomeScreenState extends State<HomeScreen> {

    @override
    Widget build(BuildContext context) {

      return MultiProvider(providers: [
        ChangeNotifierProvider.value(value: WorkSpaceProvider())
        ],
        child: Scaffold(
          appBar: AppBar(
            title: Text('首页'),
          ),
          body: ListView(
              children: <Widget>[
                dataWidget(),
                myFuncGridViewWidget(),
                sectionTitleWidget('巡店问题分布'),
                issueChartWidget(),
                sectionTitleWidget('动态'),
                statusListWidget(),
             ],
          ),
        ),
      );
  }


List<Widget> _sliverBuilder(BuildContext context, bool innerBoxIsScrolled) {
  return <Widget>[
    SliverAppBar(
      centerTitle: true, //标题居中
      expandedHeight: 200.0, //展开高度200
      backgroundColor: Colors.tealAccent,
      floating: false, //不随着滑动隐藏标题
      pinned: false, //不固定在顶部
      flexibleSpace: FlexibleSpaceBar(
        centerTitle: true,
        background: Image.asset(
          "assets/pic.jpg", 
          fit: BoxFit.cover,
        ),
      ),
    )
  ];
}



  Widget dataWidget(){
      return Stack(
        children: <Widget>[
          Column(
            children: <Widget>[
              Image.asset('images/banner.png'),
              Container(
                height: 45,
              )
            ],
          ),
           Positioned(
             bottom: 10,
             left: 10,
             right: 10,
             child: Container(
                height: 90,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(7),
                  // 阴影位置由offset决定 阴影模糊层度由blurRadius大小决定（大就更透明更扩散）阴影模糊大小由spreadRadius决定
                  boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.04),offset: Offset(5.0, 5.0), blurRadius: 5.0, spreadRadius: 2.0)]
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    dataDetailWidget('1234','当日客流'),
                    dataDetailWidget('1234','当日销售额'),
                  ],
                ),
              ),
           )
        ],
      );
  }

  Widget dataDetailWidget(String value,String title){
    return Container(
      margin: EdgeInsets.only(top: 20),
      height: 70,
      child: Column(
        children: <Widget>[
          Text(
            value,
            style: TextStyle(color: Color.fromRGBO(46, 137, 250, 1),fontSize: 30),
          ),
          Text(
            title,
            style: TextStyle(color: Colors.black54,fontSize: 13),
          ),
        ],
      ),
    );
  }

  Widget myFuncGridViewWidget(){

    return FutureBuilder(
      future: fetchUserMenus(),
      builder: (context,snapshot){

        return Consumer<WorkSpaceProvider>(
          builder: (context,WorkSpaceProvider provider,clid){

            List<WorkSpaceDetailModel> userMenus = [];

            if (snapshot.hasData){
              
              Map userMap = {};
              for (var data in snapshot.data) {
                userMap = data;
              }
              List menus = userMap['menus'];
              menus.forEach((value){
                WorkSpaceDetailModel model = WorkSpaceDetailModel.fromJson(value);
                userMenus.add(model);
              });

              WorkSpaceDetailModel moreModel = WorkSpaceDetailModel();
              moreModel.name = '更多';
              moreModel.router = 'MoreFunc';
              userMenus.add(moreModel);

              provider.setUserMenus(userMenus);

              return Container(
                color: Colors.white,
                child: GridView.builder(
                  shrinkWrap: true,
                  // 禁止滚动
                  physics: NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                  itemCount: provider.userMenusList.length,
                  // 构建一个横轴固定数量Widget
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    //横轴元素个数
                    crossAxisCount:5,
                    //纵轴间距
                    mainAxisSpacing:0,
                    //横轴间距
                    crossAxisSpacing:0,
                    // 纵横轴的比例(宽：高 = childAspectRatio）
                    childAspectRatio:1
                  ),
                  itemBuilder: (BuildContext context,int index){
                    return myFuncGridItemWidget(provider.userMenusList[index]);
                  },
                )
              );
            }else {
              return Center(child: Text('加载中'));
            }
          },
          
        );
      },
    );
  }

  Widget myFuncGridItemWidget(WorkSpaceDetailModel detailModel){
    return InkWell(
      onTap: () {
        Application.router.navigateTo(context, Routes.workSpace,transition: TransitionType.native);
      },
      child: Container(
        padding: EdgeInsets.only(top: 10),
        child: Column(
          children: <Widget>[
            Image.asset(
              'images/' + detailModel.router + '.png',
            ),
            Container(
              padding: EdgeInsets.only(top: 10),
              child: Text(detailModel.name),
            )
          ],
        ),
      ),
    );
  }

  Widget issueChartWidget(){
    return Container(
      height: 210,
      decoration: BoxDecoration(color: Colors.white),
      child: Container(
        child: HorizontalBarCharts(),
      ),
    );
  }

  Widget sectionTitleWidget(String title){
    return Container(
      height: 60,
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Container(
            height: 10,
            decoration: BoxDecoration(color: Color.fromRGBO(235, 239, 244, 1)),
          ),
          Stack(
            children: <Widget>[
              Container(
                height: 50,
              ),
              Positioned(
                left: 20,
                top: 15,
                child: Text(title,style: TextStyle(fontSize: 17,fontWeight: FontWeight.w500)),
              ),
              Positioned(
                right: 20,
                top: 15,
                child: Text('更多'),
              )
            ],
          )
        ],
      ),
    );
  }

  Widget statusListWidget(){
    return Container(
      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      child: ListView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemCount: 10,
        itemBuilder: (BuildContext context,int index){
          return statusListItemWidget();
        },
      ),
    );
  }
  
  Widget statusListItemWidget(){
    return Container(
      decoration: BoxDecoration(color: Colors.white,),
      padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
      child: Container(
        padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(7),
        // 阴影位置由offset决定 阴影模糊层度由blurRadius大小决定（大就更透明更扩散）阴影模糊大小由spreadRadius决定
        boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.1),offset: Offset(0.0, 1.0), blurRadius: 5.0, spreadRadius: 2)]
        ),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Image.asset('images/message_task.png'),
                Container(
                  padding: EdgeInsets.only(left: 10),
                  child: Text('任务巡店',style: TextStyle(fontWeight: FontWeight.bold)),
                ),
              ],
            ),
            Container(
              height: 0.5,
              margin: EdgeInsets.only(top: 10),
              decoration: BoxDecoration(color: Color.fromRGBO(10, 10, 10, 0.1)),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                  child: Column(
                    children: <Widget>[
                      Text('成都市高新区盯盯旗舰店',style: TextStyle(height: 1,),),
                      Text('2018.01.01-2018.02.02',style: TextStyle(height: 1)),
                    ],
                  ),
                ),
                Text('3小时前')
              ],
            )
          ],
        )
      )
    );
  }

  Future fetchUserMenus() async {
    NetRequest userApi = NetRequest();
    String userUrl = 'menus/owner?type=USER';
    Response userResponse = await userApi.get(userUrl);
    return userResponse.data;
  }

}